const NProgress = require('nprogress')
import 'nprogress/nprogress.css'
import store from './store'
const { default: router } = require('./router')

// 关闭右侧小圆环
NProgress.configure({
  showSpinner: false
})

// 白名单
const whiteList = ['/login']

/**
 * 路由全局前置守卫
 */
router.beforeEach((to, from, next) => {
  // 开启进度条
  NProgress.start()
  const token = store.getters.token

  if (token) {
    // 如果是去登录页 拦截去首页
    if (to.path === '/login') {
      next('/')
      // 细节: 一旦拦截就不会进入后置守卫
      NProgress.done()
    } else {
      if (!store.getters.userInfo?.userId) {
        store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      // 细节: 一旦拦截就不会进入后置守卫
      NProgress.done()
    }
  }
})
/**
 * 路由全局后置守卫
 */
router.afterEach(() => {
  // 关闭进度条
  NProgress.done()
})
